package net.wanho.customerManage.dao.impl;

import net.wanho.commons.exception.DaoException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.commons.util.JDBCTemplate;
import net.wanho.customerManage.dao.BusinessDao;
import net.wanho.customerManage.po.Business;
import net.wanho.customerManage.query.BusinessQuery;
import net.wanho.customerManage.vo.BusinessVo;

import java.util.List;

public class BusinessDaoImpl implements BusinessDao {
    private JDBCTemplate jdbcTemplate = BeanFactory.getBean(JDBCTemplate.class) ;
    @Override
    public long selectTotalByCondBusiness(BusinessQuery businessQuery) throws DaoException {
        final String SELECT_COUNT_BUSINESS_SQL = "SELECT COUNT(0) FROM business WHERE 1=1 " ;
        try {
            return jdbcTemplate.queryForInt(SELECT_COUNT_BUSINESS_SQL);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DaoException(e.getMessage()) ;
        }
    }

    @Override
    public List<BusinessVo> selectCurrPageByCondBusiness(int pageNum, int pageSize, BusinessQuery businessQuery) throws DaoException {
        final String SELECT_CURR_PAGE_BUSINESS_SQL ="SELECT\n" +
                "  b.BUSINESS_ID BUSINESSID,\n" +
                "  b.CUSTOMER_ID CUSTOMERID,\n" +
                "  b.LINK_MAIN_ID LINKMAINID,\n" +
                "  b.BUSINESS_NAME BUSINESSNAME,\n" +
                "  b.BUSINESS_TYPE_ID BUSINESSTYPEID,\n" +
                "  b.STATUS STATUS,\n" +
                "  b.BUSINESS_SOURCE_ID BUSINESSSOURCEID,\n" +
                "  b.PERSALE_PRICE PERSALEPRICE,\n" +
                "  b.PROBABILITY PROBABILITY,\n" +
                "  b.VISIT_TIME VISITTIME,\n" +
                "  b.VISIT_CONTENT VISITCONTENT,\n" +
                "  b.UPDATE_TIME UPDATETIME,\n" +
                "  b.EMPLOYEE_ID EMPLOYEEID,\n" +
                "  l.NAME linkManName,\n" +
                "  c.CUSTOMER_NAME customerName,\n" +
                "  bs.SOURCE_NAME businessSourceName,\n" +
                "  bt.TYPE_NAME businessTypeName,\n" +
                "  e.EMPLOYEE_NAME employeeName\n" +
                "FROM business b\n" +
                "  LEFT JOIN link_man l\n" +
                "    ON b.LINK_MAIN_ID = l.LINK_MAIN_ID\n" +
                "  LEFT JOIN customer c\n" +
                "    ON b.CUSTOMER_ID = c.CUSTOMER_ID\n" +
                "  LEFT JOIN business_source bs\n" +
                "    ON b.BUSINESS_SOURCE_ID = bs.SOURCE_ID\n" +
                "  LEFT JOIN business_type bt\n" +
                "    ON b.BUSINESS_TYPE_ID = bt.TYPE_ID\n" +
                "  LEFT JOIN employee e\n" +
                "    ON b.EMPLOYEE_ID = e.EMPLOYEE_ID\n" +
                "WHERE 1 = 1 limit ?,?" ;
        Object[] params = {(pageNum-1)*pageSize,pageSize} ;
        try {
            return jdbcTemplate.queryForList(SELECT_CURR_PAGE_BUSINESS_SQL,BusinessVo.class,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DaoException(e.getMessage()) ;
        }
    }

    @Override
    public Business selectByIdBusiness(Integer businessId) throws DaoException {
        final String SELECT_BY_ID_BUSINESS_SQL ="SELECT\n" +
                "  b.BUSINESS_ID BUSINESSID,\n" +
                "  b.CUSTOMER_ID CUSTOMERID,\n" +
                "  b.LINK_MAIN_ID LINKMAINID,\n" +
                "  b.BUSINESS_NAME BUSINESSNAME,\n" +
                "  b.BUSINESS_TYPE_ID BUSINESSTYPEID,\n" +
                "  b.STATUS STATUS,\n" +
                "  b.BUSINESS_SOURCE_ID BUSINESSSOURCEID,\n" +
                "  b.PERSALE_PRICE PERSALEPRICE,\n" +
                "  b.PROBABILITY PROBABILITY,\n" +
                "  b.VISIT_TIME VISITTIME,\n" +
                "  b.VISIT_CONTENT VISITCONTENT,\n" +
                "  b.UPDATE_TIME UPDATETIME,\n" +
                "  b.EMPLOYEE_ID EMPLOYEEID \n" +
                "FROM business b\n" +
                "WHERE BUSINESS_ID = ?" ;
        try {
            return jdbcTemplate.queryForObject(SELECT_BY_ID_BUSINESS_SQL,Business.class,businessId);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DaoException(e.getMessage()) ;
        }
    }

    @Override
    public void updateBusiness(Business business) throws DaoException {
       final String UPDATE_BUSINESS_SQL = "UPDATE business SET  CUSTOMER_ID = ? , LINK_MAIN_ID = ? , BUSINESS_NAME = ? , BUSINESS_TYPE_ID = ? , STATUS = ? , BUSINESS_SOURCE_ID = ? , PERSALE_PRICE = ? , PROBABILITY = ? , VISIT_TIME = ? , VISIT_CONTENT = ? , UPDATE_TIME = ? , EMPLOYEE_ID = ? WHERE BUSINESS_ID = ?" ;
       Object[] params = {business.getCustomerId(),business.getLinkMainId(),business.getBusinessName(),business.getBusinessTypeId(),business.getStatus(),business.getBusinessSourceId(),business.getPersalePrice(),business.getProbability(),business.getVisitTime(),business.getVisitContent(),business.getUpdateTime(),business.getEmployeeId(),business.getBusinessId()} ;
        try {
            jdbcTemplate.update(UPDATE_BUSINESS_SQL,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DaoException(e.getMessage()) ;
        }
    }
}
